package com.xz.stu.server.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xz.stu.server.entity.ScheduleTimeSlot;
import com.xz.stu.server.mapper.ScheduleTimeSlotMapper;
import com.xz.stu.server.service.ScheduleTimeSlotService;
import com.xz.stu.server.vo.scheduleTimeSolt.TimeSlotCreateReqVO;
import com.xz.stu.server.vo.scheduleTimeSolt.TimeSlotPageReqVO;
import com.xz.stu.server.vo.scheduleTimeSolt.TimeSlotUpdateReqVO;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScheduleTimeSlotServiceImpl extends ServiceImpl<ScheduleTimeSlotMapper, ScheduleTimeSlot>
        implements ScheduleTimeSlotService {

    @Override
    public Long createTimeSlot(TimeSlotCreateReqVO createReqVO) {
        // 校验节次名称是否已存在
        validateTimeSlotNameUnique(null, createReqVO.getName());

        ScheduleTimeSlot timeSlot = new ScheduleTimeSlot()
                .setName(createReqVO.getName())
                .setStartTime(createReqVO.getStartTime())
                .setEndTime(createReqVO.getEndTime())
                .setSort(createReqVO.getSort());

        baseMapper.insert(timeSlot);
        return timeSlot.getId();
    }

    @Override
    public void updateTimeSlot(TimeSlotUpdateReqVO updateReqVO) {
        // 校验节次是否存在
        ScheduleTimeSlot timeSlot = validateTimeSlotExists(updateReqVO.getId());
        // 校验节次名称是否已存在
        validateTimeSlotNameUnique(updateReqVO.getId(), updateReqVO.getName());

        ScheduleTimeSlot updateObj = new ScheduleTimeSlot()
                .setId(updateReqVO.getId())
                .setName(updateReqVO.getName())
                .setStartTime(updateReqVO.getStartTime())
                .setEndTime(updateReqVO.getEndTime())
                .setSort(updateReqVO.getSort());

        baseMapper.updateById(updateObj);
    }

    @Override
    public void deleteTimeSlot(Long id) {
        // 校验节次是否存在
        validateTimeSlotExists(id);
        baseMapper.deleteById(id);
    }

    @Override
    public ScheduleTimeSlot getTimeSlot(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageResult<ScheduleTimeSlot> getTimeSlotPage(TimeSlotPageReqVO pageVO) {
        LambdaQueryWrapper<ScheduleTimeSlot> queryWrapper = new LambdaQueryWrapper<>();
        // 按名称模糊查询
        if (pageVO.getName() != null) {
            queryWrapper.like(ScheduleTimeSlot::getName, pageVO.getName());
        }
        // 按排序字段排序
        queryWrapper.orderByAsc(ScheduleTimeSlot::getSort);

        IPage<ScheduleTimeSlot> pageResult = baseMapper.selectPage(new Page<>(pageVO.getPageNo(), pageVO.getPageSize()), queryWrapper);
        return new PageResult<>(pageResult.getRecords(), pageResult.getTotal());
    }

    @Override
    public List<ScheduleTimeSlot> getAllTimeSlots() {
        LambdaQueryWrapper<ScheduleTimeSlot> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(ScheduleTimeSlot::getSort);
        return baseMapper.selectList(queryWrapper);
    }

    private ScheduleTimeSlot validateTimeSlotExists(Long id) {
        ScheduleTimeSlot timeSlot = baseMapper.selectById(id);
        if (timeSlot == null) {
            throw new RuntimeException("节次不存在");
        }
        return timeSlot;
    }

    private void validateTimeSlotNameUnique(Long id, String name) {
        LambdaQueryWrapper<ScheduleTimeSlot> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ScheduleTimeSlot::getName, name);
        if (id != null) {
            queryWrapper.ne(ScheduleTimeSlot::getId, id);
        }

        Long count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new RuntimeException("节次名称已存在");
        }
    }
}
